package can.bishe.modules.exam.controller;

import java.time.LocalDateTime;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import can.bishe.common.validator.ValidatorUtils;
import can.bishe.modules.exam.entity.ExamSubjectEntity;
import can.bishe.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import can.bishe.modules.exam.entity.ExamTestpaperEntity;
import can.bishe.modules.exam.service.ExamTestpaperService;
import can.bishe.common.utils.PageUtils;
import can.bishe.common.utils.R;



/**
 * 试卷信息
 *
 * @author can
 * @email 822024417@qq.com
 * @date 2021-04-28 23:03:48
 */
@RestController
@RequestMapping("exam/examtestpaper")
public class ExamTestpaperController extends AbstractController {
    @Autowired
    private ExamTestpaperService examTestpaperService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = examTestpaperService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/tpList")
    public R tpList(@RequestParam Map<String, Object> params){
        List<ExamTestpaperEntity> examTestpaperEntities = examTestpaperService.list(new QueryWrapper<ExamTestpaperEntity>().groupBy("tp_name"));

        return R.ok().put("data", examTestpaperEntities);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{tpId}")
    public R info(@PathVariable("tpId") Long tpId){
        ExamTestpaperEntity examTestpaper = examTestpaperService.getById(tpId);
        QueryWrapper qw = new QueryWrapper();
        qw.eq("tp_name",examTestpaper.getTpName());
        List<ExamTestpaperEntity> tpList = examTestpaperService.list(qw);
        List<Long> subjectIdList = new ArrayList<>();
        for (ExamTestpaperEntity tpOne:tpList) {
            subjectIdList.add(tpOne.getTpSubjectid());
        }
        examTestpaper.setSubjectList(subjectIdList);
        return R.ok().put("examTestpaper", examTestpaper);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ExamTestpaperEntity examTestpaper){
        String realName = getUser().getRealname();
        Date nowTime = new Date();
        List<Long> subjectIdList = examTestpaper.getSubjectList();
        for (Long subjectId:subjectIdList) {
            ExamTestpaperEntity examTestpaperEntityOne = new ExamTestpaperEntity();
            examTestpaperEntityOne.setTpName(examTestpaper.getTpName());
            examTestpaperEntityOne.setTpCreatuser(realName);
            examTestpaperEntityOne.setTpCreatetime(nowTime);
            examTestpaperEntityOne.setTpSubjectid(subjectId);
            examTestpaperService.save(examTestpaperEntityOne);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ExamTestpaperEntity examTestpaper){
        ValidatorUtils.validateEntity(examTestpaper);
        QueryWrapper qw = new QueryWrapper();
        qw.eq("tp_name",examTestpaper.getTpName());
        examTestpaperService.remove(qw);
        String realName = getUser().getRealname();
        Date nowTime = new Date();
        List<Long> subjectIdList = examTestpaper.getSubjectList();
        for (Long subjectId:subjectIdList) {
            ExamTestpaperEntity examTestpaperEntityOne = new ExamTestpaperEntity();
            examTestpaperEntityOne.setTpName(examTestpaper.getTpName());
            examTestpaperEntityOne.setTpCreatuser(realName);
            examTestpaperEntityOne.setTpCreatetime(nowTime);
            examTestpaperEntityOne.setTpSubjectid(subjectId);
            examTestpaperService.save(examTestpaperEntityOne);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] tpIds){
        List<Long> idList = Arrays.asList(tpIds);
        for (Long id:idList) {
            ExamTestpaperEntity examTestpaperEntity = examTestpaperService.getById(id);
            QueryWrapper qw = new QueryWrapper();
            qw.eq("tp_name",examTestpaperEntity.getTpName());
            examTestpaperService.remove(qw);
        }
        return R.ok();
    }

}
